package com.yanqu.road.server.manger.activity.dafuweng;

import com.yanqu.road.entity.activity.dafuweng.DFWUserData;
import com.yanqu.road.entity.activity.dafuweng.LuZhangData;
import com.yanqu.road.entity.activity.dafuweng.LuZhangResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.monopoly.MonopolyProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * @author ：zfr
 * @description：路障逻辑
 * @date ：2021/1/22 10:38
 */
public class LuZhangLogic {
    /**
     * 移动到路障
     *
     * @param randomNum
     * @param dfwUserData
     * @return
     */
    public static LuZhangResult moveLuZhang(int randomNum, DFWUserData dfwUserData) {
        LuZhangResult luZhangResult = new LuZhangResult();
        luZhangResult.setPositionIndex(-1);
        //密道路障
        List<LuZhangData> secretRoadLuZhangDataList = new ArrayList<>();
        //普通区域路障
        List<LuZhangData> normalLuZhangDataList = new ArrayList<>();
        int step = 0;
        for (LuZhangData luZhangData : dfwUserData.getLuZhangDataList()) {
            if (luZhangData.isSecretRoad()) {
                secretRoadLuZhangDataList.add(luZhangData);
            } else if (!luZhangData.isSecretRoad()) {
                normalLuZhangDataList.add(luZhangData);
            }
        }
        LuZhangData luZhangData = new LuZhangData();
        //有密道路障 可以判断玩家也正处于密道事件
        if (DaFuWengMgr.isSecretRoadEvent(dfwUserData) && secretRoadLuZhangDataList.size() > 0) {
            luZhangData = secretRoadLuZhangDataList.stream().min(Comparator.comparingInt(LuZhangData::getPositionIndex)).get();
            step = luZhangData.getPositionIndex() - Integer.parseInt(dfwUserData.getExpandParam());
            luZhangResult.setPositionIndex(luZhangData.getPositionIndex());
        } else if (normalLuZhangDataList.size() > 0) {
            //是否在密道内
            if (DaFuWengMgr.isSecretRoadEvent(dfwUserData)) {
                luZhangData = getForwardLuZhangData(normalLuZhangDataList, dfwUserData, true);
                int linkPosition = DaFuWengMgr.getDfwConfig().getDfwEventMap().get(dfwUserData.getEventId()).getParamList().get(1);
                int left = dfwUserData.getUserSecretRoadList().size() - 1 - Integer.parseInt(dfwUserData.getExpandParam());
                step = luZhangData.getPositionIndex() - linkPosition + 1 + left;
            } else {
                luZhangData = getForwardLuZhangData(normalLuZhangDataList, dfwUserData, false);
                if (luZhangData.getPositionIndex() > dfwUserData.getPositionIndex()) {
                    step = luZhangData.getPositionIndex() - dfwUserData.getPositionIndex();
                } else {
                    step = luZhangData.getPositionIndex() + DaFuWengMgr.StepsNum - dfwUserData.getPositionIndex();
                }

            }
        }
        if (step > 0 && randomNum >= step) {
            luZhangResult.setLuZhang(true);
            luZhangResult.setStepNum(step);
            luZhangResult.setPositionIndex(luZhangData.getPositionIndex());
            luZhangResult.setSecretRoad(luZhangData.isSecretRoad());
            if (randomNum == step) {
                luZhangResult.setSmallPosition(true);
            }
        }
        return luZhangResult;
    }

    private static LuZhangData getForwardLuZhangData(List<LuZhangData> normalLuZhangDataList, DFWUserData userData, boolean isSecretRoad) {
        List<Integer> diffStep = new ArrayList<>();
        int positionIndex = 0;
        //人偶在密道内 路障又在密道外
        if (isSecretRoad) {
            //出口处坐标
            int linkPosition = DaFuWengMgr.getDfwConfig().getDfwEventMap().get(userData.getEventId()).getParamList().get(1);
            //转换成普通区域坐标
            positionIndex = linkPosition - (6 - Integer.parseInt(userData.getExpandParam()));
        } else {
            positionIndex = userData.getPositionIndex();
        }
        for (LuZhangData luZhangData : normalLuZhangDataList) {
            if (luZhangData.getPositionIndex() > positionIndex) {
                diffStep.add(luZhangData.getPositionIndex() - positionIndex);
            } else {
                diffStep.add(luZhangData.getPositionIndex() + DaFuWengMgr.StepsNum - positionIndex);
            }
        }
        int index = diffStep.indexOf(Collections.min(diffStep));
        return normalLuZhangDataList.get(index);
    }

    /**
     * 移除路障
     * @param luZhangResult
     * @param dfwUserData
     */
    public static void removeLuZhang(LuZhangResult luZhangResult, DFWUserData dfwUserData) {
        List<LuZhangData> luZhangDataList = dfwUserData.getLuZhangDataList();
        for (LuZhangData luZhangData : luZhangDataList) {
            if (luZhangData.isSecretRoad() == luZhangResult.isSecretRoad() && luZhangData.getPositionIndex() == luZhangResult.getPositionIndex()) {
                luZhangDataList.remove(luZhangData);
                break;
            }
        }
        dfwUserData.setUpdateOption();
    }

    /**
     * 设置路障
     *
     * @param param   (前方路障距离)
     * @param player
     * @param respMsg
     */
    public static void setLuZhang(String param, GamePlayer player, int cardId, MonopolyProto.UseCardRespMsg.Builder respMsg) {
        int forwardIndex = Integer.parseInt(param);  //前方路障距离
        if (forwardIndex <= 0 || forwardIndex > 6) {
            respMsg.setRet(GameErrorCode.E_ACTIVITY_DFW_LUZHANG_POSITION_ERROR);
            return;
        }
        int positionIndex;
        boolean isSecretRoad = false;
        DFWUserData userData = DaFuWengMgr.getUserData(DaFuWengMgr.getDfwConfig().getActivityInfo().getActivityId(), player.getUserId());
        //判断是否处于密道之内
        if (DaFuWengMgr.isSecretRoadEvent(userData)) {
            //人偶处于密道机关
            if (Integer.parseInt(userData.getExpandParam()) + forwardIndex <= userData.getUserSecretRoadList().size() - 1 || Integer.parseInt(userData.getExpandParam()) == -1) {
                //路障设置在密道内
                isSecretRoad = true;
                positionIndex = Integer.parseInt(userData.getExpandParam()) + forwardIndex;
            } else {
                //路障在密道外
                int leftIndex = forwardIndex - (userData.getUserSecretRoadList().size() - 1 - Integer.parseInt(userData.getExpandParam()));
                int linkPosition = DaFuWengMgr.getDfwConfig().getDfwEventMap().get(userData.getEventId()).getParamList().get(1);
                positionIndex = linkPosition - 1 + leftIndex;
            }
        } else {
            //人偶不在密道机关
            if (userData.getPositionIndex() + forwardIndex >= DaFuWengMgr.StepsNum) {
                // 超圈
                positionIndex = userData.getPositionIndex() + forwardIndex - DaFuWengMgr.StepsNum;
            } else {
                positionIndex = userData.getPositionIndex() + forwardIndex;
            }
        }

        for (LuZhangData luZhangData : userData.getLuZhangDataList()) {
            if (luZhangData.isSecretRoad() == isSecretRoad && luZhangData.getPositionIndex() == positionIndex) {
                respMsg.setRet(GameErrorCode.E_ACTIVITY_DFW_LUZHANG_IS_EXIST);
                return;
            }
        }
        if (!player.getModule(CurrencyModule.class).removeCurrency(new Property(cardId, BigInteger.ONE), eLogMoneyType.DaFuWeng, eLogMoneyType.DaFuWengUseCardCost)) {
            respMsg.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
            return;
        }
        LuZhangData luZhangData = new LuZhangData();
        luZhangData.setPositionIndex(positionIndex);
        luZhangData.setSecretRoad(isSecretRoad);
        userData.getLuZhangDataList().add(luZhangData);
        userData.setUpdateOption();
        respMsg.setRet(0);
    }
}
